package com.itheima.xml;

import com.itheima.domain.Student;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/*
dom4J 解析xml
 */
public class Demo01 {


    public static void main(String[] args) throws DocumentException {

        ArrayList<Student> list = new ArrayList<>();
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new File("day18\\xml\\students.xml"));
        Element root = document.getRootElement();
        List elements = root.elements();
        System.out.println(elements.size());
        List<Element> students = root.elements("student");
        System.out.println(students.size());
        for (Element student : students) {
            Attribute idAttr = student.attribute("id");
            String id = idAttr.getValue();
            Element nameELement = student.element("name");
            String name = nameELement.getText();
            Element ageELement = student.element("age");
            String age = ageELement.getText();
            Student s = new Student(id, name, Integer.parseInt(age));
            list.add(s);

        }


        System.out.println(list);
    }
}
